package com.guigu.youxuan.controller;

import com.guigu.youxuan.common.utils.PageUtils;
import com.guigu.youxuan.pojo.BackUser;
import com.guigu.youxuan.pojo.CartInfo;
import com.guigu.youxuan.pojo.dto.CartInfoDto;
import com.guigu.youxuan.service.ICartInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liweixin
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/cartInfo")
public class CartInfoController {
    @Autowired
    private ICartInfoService iCartInfoService;

    /**
     * 我的购物车
     * @param sysUsersId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequestMapping("/selCarts/{sysUsersId}/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<CartInfoDto> selCarts(@PathVariable int sysUsersId, @PathVariable int pageNo, @PathVariable int pageSize){
        PageUtils<CartInfoDto> cartInfos = iCartInfoService.cartInfoList(sysUsersId,pageNo, pageSize);
        return cartInfos;
    }

    /**
     * 购物车数量
     * @param sysUsersId
     * @return
     */
    @RequestMapping("/selCarts/{sysUsersId}")
    @ResponseBody
    public String selCarts(@PathVariable int sysUsersId){
        String selCarts = iCartInfoService.selCarts(sysUsersId);
        return selCarts;
    }

    /**
     * 单个删除购物车
     * @param cartId
     * @return
     */
    @RequestMapping("/delCartInfo/{cartId}")
    @ResponseBody
    public void delCartInfo(@PathVariable int cartId) {
        iCartInfoService.delCartInfo(cartId);
    }

    /**
     * 加入购物车
     * @param sysUsersId
     * @param goodsId
     * @param goodsCount
     * @return
     */
    @RequestMapping("/addToCart/{sysUsersId}/{goodsId}/{goodsCount}")
    @ResponseBody
    public String addToCart(@PathVariable int sysUsersId, @PathVariable int goodsId ,@PathVariable int goodsCount) {
        //查询购物车中是否已经存在该商品
        CartInfo cartInfos = iCartInfoService.getCartItem(sysUsersId, goodsId);

        if(cartInfos != null) {
            //如果购物车中已经存在该商品，则更新数量
            iCartInfoService.updateCartCount(goodsId,goodsCount);
        } else {
            //如果购物车中不存在该商品，则插入新记录
            iCartInfoService.addToCart(sysUsersId, goodsId, goodsCount);
        }
        return "加入成功！";
    }
}
